package com.antis.olsl.magic.profit.cash;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.antis.olsl.base.BaseFragment;
import com.antis.olsl.magic.databinding.FragmentGrossProfitBinding;
import com.antis.olsl.magic.response.GetLevel5ProfitCashFlowResp;
import com.trello.rxlifecycle2.LifecycleTransformer;

import java.util.HashMap;
import java.util.Map;

import timber.log.Timber;

/**
 * 资金周转-毛利润Fragment
 */
public class GrossProfitFragment extends BaseFragment<FragmentGrossProfitBinding> implements GrossProfitContract.View {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private static final String ARG_PARAM3 = "param3";
    private static final String ARG_PARAM4 = "param4";

    private String mSalesroomId;
    private String mStartTime;
    private String mEndTime;
    private int mType;

    private GrossProfitPresenter mPresenter;

    private boolean isLoadData = false;//数据是否加载

    public GrossProfitFragment() {
    }

    public static GrossProfitFragment newInstance(String salesroomId, String startTime, String endTime, int type) {
        GrossProfitFragment fragment = new GrossProfitFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, salesroomId);
        args.putString(ARG_PARAM2, startTime);
        args.putString(ARG_PARAM3, endTime);
        args.putInt(ARG_PARAM4, type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mSalesroomId = getArguments().getString(ARG_PARAM1);
            mStartTime = getArguments().getString(ARG_PARAM2);
            mEndTime = getArguments().getString(ARG_PARAM3);
            mType = getArguments().getInt(ARG_PARAM4);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null != mPresenter) {
            mPresenter.unSubscribe();
        }
    }

    @Override
    protected FragmentGrossProfitBinding getViewBinding() {
        return FragmentGrossProfitBinding.inflate(getLayoutInflater(), baseBinding.getRoot(), true);
    }

    @Override
    protected void initView(View view) {
        initPresenter();
    }

    private void initPresenter() {
        mPresenter = new GrossProfitPresenter();
        mPresenter.takeView(this);
    }

    @Override
    protected void loadData() {
        loadData(mSalesroomId, mStartTime, mEndTime, mType);
        Timber.tag("hhh").e("GrossProfitFragment loadData type= " + mType);
    }

    public void loadData(String salesroomId, String startTime, String endTime, int type) {
        if (!isLoadData) {
            mSalesroomId = salesroomId;
            mStartTime = startTime;
            mEndTime = endTime;
            mType = type;
            isLoadData = true;
            Map<String, Object> map = new HashMap<>(4);
            map.put("shopId", salesroomId);
            map.put("startTime", startTime);
            map.put("endTime", endTime);
            map.put("type", type);
            mPresenter.getLevel5ProfitCashFlow(map);
        }
    }

    @Override
    public void getLevel5ProfitCashFlowSuccess(GetLevel5ProfitCashFlowResp.ContentBean bean) {
        isLoadData = false;

        setTopInfo(bean);

        setContent(bean);
    }

    private void setTopInfo(GetLevel5ProfitCashFlowResp.ContentBean bean) {
        // sku数
        if (!TextUtils.isEmpty(bean.skuNumber)) {
            viewBinding.textSkuNum.setText(bean.skuNumber);
        } else {
            viewBinding.textSkuNum.setText("0");
        }

        // 区间值
        if (!TextUtils.isEmpty(bean.intervalValue)) {
            viewBinding.textIntervalValue.setText(bean.intervalValue);
        } else {
            viewBinding.textIntervalValue.setText("-");
        }

        // 占比
        if (!TextUtils.isEmpty(bean.proportion)) {
            viewBinding.textProportion.setText(bean.proportion);
        } else {
            viewBinding.textProportion.setText("-");
        }
    }

    private void setContent(GetLevel5ProfitCashFlowResp.ContentBean bean) {
        // 销售数量
        if (!TextUtils.isEmpty(bean.salesVolumeProportion)) {
            viewBinding.textSalesVolumeProportion.setText(bean.salesVolumeProportion);
        } else {
            viewBinding.textSalesVolumeProportion.setText("-");
        }

        if (!TextUtils.isEmpty(bean.salesVolume)) {
            viewBinding.textSalesVolume.setText(bean.salesVolume);
        } else {
            viewBinding.textSalesVolume.setText("0");
        }

        // 销售金额
        if (!TextUtils.isEmpty(bean.salesAmountProportion)) {
            viewBinding.textSalesAmountProportion.setText(bean.salesAmountProportion);
        } else {
            viewBinding.textSalesAmountProportion.setText("-");
        }

        if (!TextUtils.isEmpty(bean.salesAmount)) {
            viewBinding.textSalesAmount.setText(bean.salesAmount);
        } else {
            viewBinding.textSalesAmount.setText("0");
        }

        // 销售成本
        if (!TextUtils.isEmpty(bean.salesCostProportion)) {
            viewBinding.textSalesCostProportion.setText(bean.salesCostProportion);
        } else {
            viewBinding.textSalesCostProportion.setText("-");
        }

        if (!TextUtils.isEmpty(bean.salesCost)) {
            viewBinding.textSalesCost.setText(bean.salesCost);
        } else {
            viewBinding.textSalesCost.setText("0");
        }

        // 毛利额
        if (!TextUtils.isEmpty(bean.profitProportion)) {
            viewBinding.textProfitProportion.setText(bean.profitProportion);
        } else {
            viewBinding.textProfitProportion.setText("-");
        }

        if (!TextUtils.isEmpty(bean.profit)) {
            viewBinding.textProfit.setText(bean.profit);
        } else {
            viewBinding.textProfit.setText("0");
        }

        // 周转率
        if (!TextUtils.isEmpty(bean.turnover)) {
            viewBinding.textTurnover.setText(bean.turnover);
        } else {
            viewBinding.textTurnover.setText("-");
        }

        if (!TextUtils.isEmpty(bean.turnaroundTime)) {
            viewBinding.textTurnaroundTime.setText(bean.turnaroundTime);
        } else {
            viewBinding.textTurnaroundTime.setText("-");
        }

        // 单品平均贡献
        if (!TextUtils.isEmpty(bean.productAverageTotal)) {
            viewBinding.textProductAverageTotal.setText(bean.productAverageTotal);
        } else {
            viewBinding.textProductAverageTotal.setText("-");
        }

        if (!TextUtils.isEmpty(bean.productAverageProportion)) {
            viewBinding.textProductAverageProportion.setText(bean.productAverageProportion);
        } else {
            viewBinding.textProductAverageProportion.setText("-");
        }

        if (!TextUtils.isEmpty(bean.productAverage)) {
            viewBinding.textProductAverage.setText(bean.productAverage);
        } else {
            viewBinding.textProductAverage.setText("-");
        }
    }

    @Override
    public void getLevel5ProfitCashFlowFailure(String msg) {
        isLoadData = false;
        setDefaultValue();
    }

    private void setDefaultValue() {
        viewBinding.textSkuNum.setText("0");
        viewBinding.textIntervalValue.setText("-");
        viewBinding.textProportion.setText("-");
        // 销售数量
        viewBinding.textSalesVolumeProportion.setText("-");
        viewBinding.textSalesVolume.setText("0");
        // 销售金额
        viewBinding.textSalesAmountProportion.setText("-");
        viewBinding.textSalesAmount.setText("0");
        // 销售成本
        viewBinding.textSalesCostProportion.setText("-");
        viewBinding.textSalesCost.setText("0");
        // 毛利额
        viewBinding.textProfitProportion.setText("-");
        viewBinding.textProfit.setText("0");
        // 周转率
        viewBinding.textTurnover.setText("-");
        viewBinding.textTurnaroundTime.setText("-");
        // 单品平均贡献
        viewBinding.textProductAverageTotal.setText("-");
        viewBinding.textProductAverageProportion.setText("-");
        viewBinding.textProductAverage.setText("-");
    }

    @Override
    public boolean isActive() {
        return false;
    }

    @Override
    public <T> LifecycleTransformer<T> bindToLife() {
        return this.bindToLifecycle();
    }
}
